package com.project.shanxian;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;

import com.project.shanxian.databinding.ActivityMainBinding;

import java.math.BigDecimal;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    /**
     * 棉花主力
     */
    private String mianhuazhuli = "0";
    /**
     * 到厂费用
     */
    private String daochangfeiyong = "0";
    /**
     * 损耗
     */
    private String sunhao1 = "0";
    /**
     * 加工费
     */
    private String jiagongfei = "0";
    /**
     * 原料1
     */
    private String yuanliao1 = "0";
    /**
     * 比例1
     */
    private String bili1 = "0";
    /**
     * 损耗1
     */
    private String sunhaoA = "0";
    /**
     * 金额1
     */
    private String jine1 = "0";
    /**
     * 原料2
     */
    private String yuanliao2 = "0";
    /**
     * 比例2
     */
    private String bili2 = "0";
    /**
     * 损耗2
     */
    private String sunhaoB = "0";
    /**
     * 金额2
     */
    private String jine2 = "0";
    /**
     * 原料3
     */
    private String yuanliao3 = "0";
    /**
     * 比例3
     */
    private String bili3 = "0";
    /**
     * 损耗3
     */
    private String sunhaoC = "0";
    /**
     * 金额3
     */
    private String jine3 = "0";
    /**
     * 加工费2
     */
    private String jiagongfei2 = "0";
    /**
     * 价格
     */
    private String price = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        //棉花主力
        binding.et1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mianhuazhuli = s.toString().trim();
                //如果值为空，就设置为0
                if (TextUtils.isEmpty(mianhuazhuli)) {
                    mianhuazhuli = "0";
                }
                calculate();
            }
        });

        //到厂费用
        binding.et2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                daochangfeiyong = s.toString().trim();
                //如果值为空，就设置为0
                if (TextUtils.isEmpty(daochangfeiyong)) {
                    daochangfeiyong = "0";
                }
                calculate();
            }
        });

        //损耗
        binding.et3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                sunhao1 = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate();
            }
        });

        //加工费
        binding.et4.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                jiagongfei = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate();
            }
        });

        //原料1
        binding.et6.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                yuanliao1 = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate2();
            }
        });
        //比例1
        binding.et7.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                bili1 = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate2();
            }
        });
        //损耗1
        binding.et8.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                sunhaoA = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate2();
            }
        });
        //原料2
        binding.et10.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                yuanliao2 = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate2();
            }
        });
        //比例2
        binding.et11.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                bili2 = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate2();
            }
        });
        //损耗2
        binding.et12.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                sunhaoB = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate2();
            }
        });
        //原料3
        binding.et14.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                yuanliao3 = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate2();
            }
        });
        //比例3
        binding.et15.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                bili3 = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate2();
            }
        });
        //损耗3
        binding.et16.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                sunhaoC = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate2();
            }
        });
        //加工费2
        binding.et18.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                jiagongfei2 = TextUtils.isEmpty(s.toString().trim()) ? "0" : s.toString().trim();
                calculate2();
            }
        });
        //清理
        binding.btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearAll();
            }
        });
    }

    /**
     * 清理
     */
    private void clearAll() {
        mianhuazhuli = "0";
        daochangfeiyong = "0";
        sunhao1 = "0";
        jiagongfei = "0";
        yuanliao1 = "0";
        bili1 = "0";
        sunhaoA = "0";
        jine1 = "0";
        yuanliao2 = "0";
        bili2 = "0";
        sunhaoB = "0";
        jine2 = "0";
        yuanliao3 = "0";
        bili3 = "0";
        sunhaoC = "0";
        jine3 = "0";
        jiagongfei2 = "0";
        price = "0";
        binding.et1.setText("");
        binding.et2.setText("");
        binding.et3.setText("");
        binding.et4.setText("");
        binding.et5.setText("");
        binding.et6.setText("");
        binding.et7.setText("");
        binding.et8.setText("");
        binding.et9.setText("");
        binding.et10.setText("");
        binding.et11.setText("");
        binding.et12.setText("");
        binding.et13.setText("");
        binding.et14.setText("");
        binding.et15.setText("");
        binding.et16.setText("");
        binding.et17.setText("");
        binding.et18.setText("");
        binding.et19.setText("");
    }

    /**
     * 计算上半部分
     */
    private void calculate() {
        try {
            if (TextUtils.isEmpty(mianhuazhuli) || TextUtils.isEmpty(jiagongfei) || TextUtils.isEmpty(sunhao1)) {

            } else {
                //棉花主力
                BigDecimal bdA = new BigDecimal(mianhuazhuli);
                //到厂费用
                BigDecimal bdB = new BigDecimal(daochangfeiyong);
                //损耗
                BigDecimal temp = new BigDecimal(sunhao1);
                BigDecimal bdC = temp.divide(new BigDecimal("100")).add(new BigDecimal("1"));
                //加工费
                BigDecimal bdD = new BigDecimal(jiagongfei);
                //价格 =（棉花主力 + 到厂费用）* 损耗% + 加工费
                BigDecimal num1 = bdA.add(bdB).multiply(bdC).add(bdD);
                binding.et5.setText(num1.toString());
            }
        } catch (Exception e) {
            Log.e("info", e.toString());
        }
    }

    /**
     * 计算下半部分
     */
    private void calculate2() {
        try {
            if (TextUtils.isEmpty(yuanliao1) || TextUtils.isEmpty(bili1) || TextUtils.isEmpty(sunhaoA)
                    || TextUtils.isEmpty(yuanliao2) || TextUtils.isEmpty(bili2) || TextUtils.isEmpty(sunhaoB)
                    || TextUtils.isEmpty(yuanliao3) || TextUtils.isEmpty(bili3) || TextUtils.isEmpty(sunhaoC)
                    || TextUtils.isEmpty(jiagongfei2)) {

            } else {
                //原料1
                BigDecimal bdA = new BigDecimal(yuanliao1);
                //比例1
                BigDecimal temp1 = new BigDecimal(bili1);
                BigDecimal bdB = temp1.divide(new BigDecimal("100"));
                //损耗1
                BigDecimal temp2 = new BigDecimal(sunhaoA);
                BigDecimal bdC = temp2.divide(new BigDecimal("100")).add(new BigDecimal("1"));
                //金额 = 原料 * 比例 *损耗
                BigDecimal num1 = bdA.multiply(bdB).multiply(bdC);
                binding.et9.setText(num1.toString());

                //原料2
                BigDecimal bdD = new BigDecimal(yuanliao2);
                //比例2
                BigDecimal temp3 = new BigDecimal(bili2);
                BigDecimal bdE = temp3.divide(new BigDecimal("100"));
                //损耗1
                BigDecimal temp4 = new BigDecimal(sunhaoB);
                BigDecimal bdF = temp4.divide(new BigDecimal("100")).add(new BigDecimal("1"));
                //金额 = 原料 * 比例 *损耗
                BigDecimal num2 = bdD.multiply(bdE).multiply(bdF);
                binding.et13.setText(num2.toString());

                //原料3
                BigDecimal bdG = new BigDecimal(yuanliao3);
                //比例3
                BigDecimal temp5 = new BigDecimal(bili3);
                BigDecimal bdH = temp5.divide(new BigDecimal("100"));
                //损耗1
                BigDecimal temp6 = new BigDecimal(sunhaoC);
                BigDecimal bdI = temp6.divide(new BigDecimal("100")).add(new BigDecimal("1"));
                //金额 = 原料 * 比例 *损耗
                BigDecimal num3 = bdG.multiply(bdH).multiply(bdI);
                binding.et17.setText(num3.toString());

                //加工费
                BigDecimal bdJ = new BigDecimal(jiagongfei2);
                //价格 = 金额1 + 金额2 + 金额3 + 加工费
                BigDecimal num4 = num1.add(num2).add(num3).add(bdJ);
                binding.et19.setText(num4.toString());
            }
        } catch (Exception e) {
            Log.e("info", e.toString());
        }
    }
}